// @flow
import React from 'react'
import MenuSidebar from 'containers/MenuSidebarContainer'
import TaskAddPage from './TaskAddPage'
import TaskListPage from './TaskListPage'
import TaskEditPage from './TaskEditPage'
import JobFilePage from './JobFilePage'
import JobListPage from './JobListPage'
// import InterfaceData from './InterfaceData'
import ModelBase from './ModelBase'
import ModelDetailView from 'components/ModelDetailView'
import { Route } from 'react-router-dom'
import { connect } from 'react-redux'

//  新增业务数据组件
import DevicePage from '../../Business/components/DevicePage'
import CustomerPage from '../../Business/components/CustomerPage'
import CustomerAdd from '../../Business/components/CustomerAdd'
import Maintenance from '../../Business/components/Maintenance'
import Fault from '../../Business/components/Fault'
import EventLog from '../../Business/components/EventLog'
import FaultInfo from 'components/FaultInfo'
import Monitor from '../../Business/components/Monitor'
import DeviceType from '../../Business/components/DeviceType'
import DeviceTypeAdd from '../../Business/components/DeviceTypeAdd'
import DeviceAdd from '../../Business/components/DeviceAdd'
import DeviceEdit from '../../Business/components/DeviceEdit'
import DeviceTypeEdit from '../../Business/components/DeviceTypeEdit'
import DevicePointer from '../../Business/components/DevicePointer'
import CustomerEditPage from '../../Business/components/CustomerEditPage'
import MaintenanceAdd from '../../Business/components/MaintenanceAdd'
import MaintenanceEdit from '../../Business/components/MaintenanceEdit'
import FaultAdd from '../../Business/components/FaultAdd'
import FaultEdit from '../../Business/components/FaultEdit'
import DevicePointAdd from '../../Business/components/DevicePointAdd'
import DevicePointItem from '../../Business/components/DevicePointItem'
import Iframe from 'components/Iframe'

type Props = {
  history: Object,
  match: Object,
  userMenu: Object
}

type State = {
  menu: Object,
  routes: Array<Object>
}
class TaskManager extends React.Component<Props, State> {
  constructor (props: Props) {
    super(props)
    this.state = {
      menu: {},
      routes: [
        <Route key='model-detail' path={`${this.props.match.url}/models/:id`} component={ModelDetailView} />,
        <Route key={'task-edit'} path={`${this.props.match.url}/task-edit`} component={TaskEditPage} />,
        <Route key={'job-file'} path={`${this.props.match.url}/job-file`} component={JobFilePage} />,
        // 新增业务数据路由
        <Route key={'device-point-add'} path={`newUrl/device-point-add`} component={DevicePointAdd} />,
        <Route key={'device-point-item'} path={`newUrl/device-point-item`}
          component={DevicePointItem} />,
        <Route key={'dtadd'} path={`newUrl/device-type-add`} component={DeviceTypeAdd} />,
        <Route key={'dadd'} path={`newUrl/device-add`} component={DeviceAdd} />,
        <Route key={'de'} path={`newUrl/device-type-edit`} component={DeviceTypeEdit} />,
        <Route key={'device-pointer'} path={`newUrl/device-pointer`} component={DevicePointer} />,
        <Route key={'customer-add'} path={`newUrl/customer-add`} component={CustomerAdd} />,
        <Route key={'customer-edit'} path={`newUrl/customer-edit`} component={CustomerEditPage} />,
        <Route key={'device-edit'} path={`newUrl/device-edit`} component={DeviceEdit} />,
        <Route key={'maintenance-add'} path={`newUrl/maintenance-add`} component={MaintenanceAdd} />,
        <Route
          key={'maintenance-edit'}
          path={`newUrl/maintenance-edit`} component={MaintenanceEdit} />,
        <Route key={'fault-add'} path={`newUrl/fault-add`} component={FaultAdd} />,
        <Route
          key={'fault-edit'}
          path={`newUrl/fault-edit`} component={FaultEdit} />
      ]
    }
  }

  componentWillMount () {
    this.setMenu()
  }
  setMenu () {
    const mainHref = '/analysis'
    const menuData = this.props.userMenu[0].children.find(item => item.href === mainHref)
    const url = this.props.match.url
    // 数据分析菜单,需要预先定义好左侧菜单地址, 并且配套定义好对应组件供查询
    const srcArr = [`${url}/service`, `${url}/tasks`, `${url}/task-add`, `${url}/jobs`,
      `${url}/device-info`, `${url}/customer`, `${url}/maintenance`, `${url}/fault`,
      `${url}/event-log`, `${url}/monitor`, `${url}/device-type`, `${url}/faultInfo`]
    const componentArr = [ModelBase, TaskListPage, TaskAddPage, JobListPage, DevicePage,
      CustomerPage, Maintenance, Fault, EventLog, Monitor, DeviceType, FaultInfo]

    let menu = {}
    if (!menuData) return
    //  添加二级菜单
    menuData.children.forEach(item => {
      menu[item.name] = []
      item.children.forEach(children => {
        const { href, name } = children
        const componentIndex = srcArr.indexOf(href)
        if (componentIndex > -1) {
          const exact = href === `${url}/service` || href === `${url}/device-info` ? 'exact' : ''
          menu[item.name].push([name, href, componentArr[componentIndex], exact])
        }
      })
    })
    // 寻找并添加Iframe
    // const demoMenu = ['百度', '/analysis/https://www.baidu.com', Iframe]
    // menu['任务分析'].push(demoMenu)
    menuData.children.forEach(firstMenu => {
      firstMenu.children.forEach(secMenu => {
        if (secMenu.target === 'Iframe') {
          const { href, name } = secMenu
          const IframeMenu = [ name, `${mainHref}/${href}`, Iframe ]
          menu[firstMenu.name].push(IframeMenu)
        }
      })
    })
    this.setState({ menu })
    // 获取地址,默认选中第一个
    const firstSlectItemKey = Object.keys(menu)
    let firstSlectItemSrc
    for (let i = 0, length = firstSlectItemKey.length; i < length; i++) {
      if (menu[firstSlectItemKey[i]].length) {
        firstSlectItemSrc = menu[firstSlectItemKey[i]][0][1]
        break
      }
    }
    this.props.history.replace(firstSlectItemSrc)
  }
  render () {
    return (
      <MenuSidebar menu={this.state.menu} routes={this.state.routes} style={{ background: '#f5f5f5' }} />
    )
  }
}

const mapStateToProps = state => {
  return {
    userMenu: state.menu.userMenu
  }
}
const mapDispatchToProps = {
}
export default connect(mapStateToProps, mapDispatchToProps)(TaskManager)
